import Watcher from './watcher.js'

/*
 * @Author: zengwj
 * @Date: 2022-08-17 10:10:45
 * @LastEditors: zengwj
 * @LastEditTime: 2022-08-17 14:13:27
 * @Description:
 */
export default function initComputed(vm) {
	const computed = vm.$options.computed
	const watcher = (vm._watcher = Object.create(null))
	for (let key in computed) {
		// 实例化Watcher ，回调函数默认执行
		watcher[key] = new Watcher(computed[key], { lazy: true }, vm)
		// key 代理到vm 实例上 即this.xx 访问
		defineComputed(vm, key)
	}
}
function defineComputed(vm, key) {
	Object.defineProperty(vm, key, {
		get: function () {
			const watcher = vm._watcher[key]
			if (watcher.dirty) {
				watcher.evalute()
			}
			return watcher.value
		},
		set: function () {
			console.log('no setter')
		},
	})
}
